#!/bin/bash
#
# Copyright (C) 2009 Muhammed Uluyol
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#   GNU General Public License for more details.
#

## Begin Config
# Prefix that all programs will be installed to (all programs will have there own subdir)
PREFIX="/opt/cents"
# Lib dir, for most 64-bit systems put lib64
LIBDIR="lib"
# Merge /bin binaries into this prefix
# false for no merging
MERGETO="/usr/local"
# Number of simultaneos builds jobs at one (cores+1)
JOBS="2"
# Flags for the c/c++ compiler
CFLAGS="$CFLAGS -pipe -O2"
CXXFLAGS="$CFLAGS $CXXFLAGS"
## End Config

remove_pkg()
{
PKG="$1"
PREFIX="$2"
MERGETO="$3"
if ! [ -d "$PREFIX/$PKG" ]; then
	echo "[ERROR] Cannot find $PREFIX/$PKG"
	update_pause "Exiting"
	exit 2
fi
if [ x$MERGETO != x"false" ]; then
	update_pause "Removing merged files"
	for bin in `ls -1 $PREFIX/$PKG/bin`; do
		sudo rm -rvf $MERGETO/bin/$bin
	done
fi
update_pause "Removing files"
sudo rm -rvf $PREFIX/$PKG
}

update_pause()
{
echo -n "-> $@"
let count=0
while [ $count -lt "7" ]; do
	sleep 0.1
	echo -n "."
	let count=$count+1
done
echo
}

build_common_one()
{
update_pause "Running 'make'"
make -j$JOBS
if [ $? != 0 ]; then
	echo "[ERROR] 'make' failed, make sure you have all of the needed dependencies"
	update_pause "Exiting"
	exit 2
fi
update_pause "Running 'sudo make install'"
sudo make install
if [ $? != 0 ]; then
	echo "[ERROR] 'sudo make install' failed, make sure you have all of the needed dependencies"
	update_pause "Exiting"
	exit 2
fi
}

build_common_two()
{
if [ x$MERGETO != x"false" ]; then
	update_pause "Merging"
	for bin in `ls -1 $__PREFIX/bin`; do
		sudo ln -vs $__PREFIX/bin/$bin $MERGETO/bin/$bin
	done
fi
}

build_cmake()
{
CPWD=$PWD
mkdir cents-build
cd cents-build
update_pause "Running 'cmake'"
if [ $LIBDIR != "lib" ]; then
	env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" PKG_CONFIG_PATH="$__PKG_CONFIG_PATH" cmake ../ -DCMAKE_INSTALL_PREFIX=$__PREFIX -DLIB_SUFFIX=$(echo $LIBDIR | sed 's|lib||g')
else
	env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" PKG_CONFIG_PATH="$__PKG_CONFIG_PATH" cmake ../ -DCMAKE_INSTALL_PREFIX=$__PREFIX
fi
if [ $? != 0 ]; then
	echo "[ERROR] 'cmake' failed, make sure you have all of the needed dependencies"
	update_pause "Exiting"
	exit 2
fi
build_common_one
build_common_two
}

build_autoconf()
{
CPWD=$PWD
update_pause "Running 'configure'"
if [ -f autogen.sh ]; then
	env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" PKG_CONFIG_PATH="$__PKG_CONFIG_PATH" ./autogen.sh --prefix=$__PREFIX --libdir=$__PREFIX/$LIBDIR
else
	env CFLAGS="$CFLAGS" CXXFLAGS="$CXXFLAGS" PKG_CONFIG_PATH="$__PKG_CONFIG_PATH" ./configure --prefix=$__PREFIX --libdir=$__PREFIX/$LIBDIR
fi
if [ $? != 0 ]; then
	echo "[ERROR] 'configure' failed, make sure you have all of the needed dependencies"
	update_pause "Exiting"
	exit 2
fi
build_common_one
build_common_two
}

build_python()
{
CPWD=$PWD
update_pause "Running 'sudo python setup.py install'"
sudo python setup.py install --prefix=$__PREFIX
if [ $? != 0 ]; then
	echo "[ERROR] 'sudo python setup.py install' failed, make sure you have all of the needed dependencies"
	update_pause "Exiting"
	exit 2
fi
build_common_two
}

get_arg()
{
(for myarg in $__ARGS; do echo $myarg; done) | grep -e $1 | sed -e s,$1,, -e s,\",,g
}

## End Config
__ARGS=$@
for arg in $__ARGS; do
	case $arg in
		--help)
			echo -e "Options:"
			echo -e "  --prefix=[PREFIX] \t\t\t Install into an alternate prefix"
			echo -e "  --libdir=[LIBDIR] \t\t\t Install into an alternate libdir"
			echo -e "  --cflags=[CFLAGS] \t\t\t Pass these flags to the c compiler"
			echo -e "  --cxxflags=[CXXFLAGS] \t\t Pass these flags to the c++ compiler"
			echo -e "  --mrgdir=[MRGDIR] \t\t\t Specify an alternate merge dir"
			echo -e "  \t\t\t\t\t false for no merging"
			echo -e "  --jobs=[JOBS] \t\t\t Number of simultaneos jobs at once"
			echo -e "  --name=[NAME] \t\t\t Name of package we are building"
			echo -e "  --remove=[PKG] \t\t\t Name of package we are deleting"
			exit 2
			;;
		--prefix=*)
			PREFIX=$(get_arg --prefix=)
			;;
		--libdir=*)
			LIBDIR=$(get_arg --libdir=)
			;;
		--cflags=*)
			CFLAGS="$CFLAGS $(get_arg --cflags=)"
			;;
		--cxxflags=*)
			CXXFLAGS="$CXXFLAGS $(get_arg --cxxflags=)"
			;;
		--mrgdir=*)
			MERGETO=$(get_arg --mrgdir=)
			;;
		--jobs=*)
			JOBS=$(get_arg --jobs=)
			;;
		--name=*|--remove=*)
			;;
		*)
			echo "$arg is not an option"
			exit 3
			;;
	esac
done

if [ x$(get_arg --name=) == x"" ]; then
	__PREFIX="$PREFIX/$(basename $PWD)"
else
	__PREFIX="$PREFIX/$(get_arg --name=)"
fi
__PKG_CONFIG_PATH="$(for pkg in `ls -1 $PREFIX 2>/dev/null`; do echo -n "$PREFIX/$pkg/$LIBDIR/pkgconfig:$PREFIX/$pkg/share/pkgconfig:"; done)$PKG_CONFIG_PATH"

if [ x$(get_arg --remove=) != x"" ]; then
	remove_pkg $(get_arg --remove=) $PREFIX $MERGETO
	exit 0
fi

if (test -f CMakeLists.txt); then
	build_cmake
elif (test -f configure) || (test -f autogen.sh); then
	build_autoconf
elif (test -f setup.py); then
	build_python
else
	echo "Don't know how to build $(basename $PWD)"
	exit 99
fi
